using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Drawing;
using GameFramework;
using GameEngine;

namespace Projet_X0
{
    public class CharacterMesh : IMesh
    {
        private Texture characterTexture = null;
        private Sprite characterSprite = null;
        private int characterID;
        private float height = 65;
        private float width = 65;

        public CharacterMesh(int characterID)
        {
            this.characterID = characterID;
        }

        public override void Render()
        {
            Position charPosition = (Position)owner.GetOwner().GetState((int)StatesID.Position);
            Point position = new Point(Display.HRES / 2 + (int)charPosition.GetPos().GetX() - (int)GameManager.GetServices().display.GetCameraPosition().GetX(), Display.VRES / 2 + (int)charPosition.GetPos().GetY() - (int)GameManager.GetServices().display.GetCameraPosition().GetY());
            double b = Math.Atan2(position.Y, position.X);
            double r = Math.Sqrt(position.X * position.X + position.Y * position.Y);
            Orientation orientation = (Orientation)owner.GetOwner().GetState((int)StatesID.Orientation);
            float angle = orientation.Get();
            Point pos = new Point((int)(r * Math.Cos(b - angle)) + position.X - (int)width / 2, (int)(r * Math.Sin(b - angle)) + position.Y - (int)height / 2);
            
            characterSprite.Begin(SpriteFlags.AlphaBlend);
            characterSprite.Draw2D(characterTexture, position, angle, pos, Color.White.ToArgb());
            characterSprite.End();
        }

        public override void Terminate()
        {
            characterSprite.Dispose();
            characterTexture.Dispose();
        }

        public override void Init()
        {
            string src = ((Persistance)GameManager.GetServices().persistance).LoadMesh(characterID);
        
            characterTexture = TextureLoader.FromFile(Display.grfDevice, src, 0, 0, 0, Usage.None, Format.A8B8G8R8, Pool.Default, Filter.None, Filter.None, System.Drawing.Color.FromArgb(0, 67, 171).ToArgb());
            characterSprite = new Sprite(Display.grfDevice);
        }
        
        public float GetHeight()
        {
            return height;
        }

        public float GetWidth()
        {
            return width;
        }

    }
}
